﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Json;
/// <summary>
/// Summary description for JsonParser
/// </summary>
public class JsonParser
{
    JsonTextParser parser = null;
    JsonObject jObject = null;

    /// <summary>
    /// Constructor
    /// </summary>
    public JsonParser()
    {
        parser = new JsonTextParser();
    }

    /// <summary>
    /// Returns the Data Part of The JSON Script
    /// </summary>
    /// <param name="Array">JSON Script is an Array or List</param>
    /// <param name="jsonText">Json Text</param>
    /// <param name="breakOn">Data Required For</param>
    /// <returns></returns>
    public string getData(Boolean Array, string jsonText, string breakOn)
    {
        jObject = parser.Parse(jsonText);
        int len = breakOn.Length;
        len += 4;

        string data = String.Empty;
        if (Array)
        {
            foreach (JsonObjectCollection jObjectCollection in jObject as JsonArrayCollection)
            {
                foreach (JsonObject json in jObjectCollection)
                {
                    string name = json.Name;
                    if (name.Equals(breakOn))
                    {
                        data = json.ToString();
                        data = data.Substring(len, data.Length - len);
                        break;
                    }
                }
            }
        }
        else
        {
            foreach (JsonObject json in jObject as JsonObjectCollection)
            {
                string name = json.Name;
                if (name.Equals(breakOn))
                {
                    string text = json.GetValue().ToString();
                    data = json.ToString();
                    data = data.Substring(len, data.Length - len);
                    break;
                }
            }
        }
        return data;
    }

    /// <summary>
    /// Returns the Person Name
    /// </summary>
    /// <param name="jsonText">Json Text</param>
    /// <returns></returns>
    public string getName(string jsonText)
    {
        string lName = String.Empty;
        string fName = String.Empty;

        jObject = parser.Parse(jsonText);
        foreach (JsonObject json in jObject as JsonObjectCollection)
        {
            string name = json.Name;
            string value = json.GetValue().ToString();
            if (name.Equals("familyName"))
                lName = value;
            else if (name.Equals("givenName"))
                fName = value;
        }
        return fName + " " + lName;
    }

    /// <summary>
    /// Returns Person Profile
    /// </summary>
    /// <param name="jsonText">Json Text</param>
    /// <returns></returns>
    public Person getProfile(string jsonText)
    {
        Person person = new Person();

        jObject = parser.Parse(jsonText);
        foreach (JsonObject json in jObject as JsonObjectCollection)
        {
            string name = json.Name;
            string value = json.GetValue().ToString();

            if (name.Equals("thumbnailUrl"))
                person.ThumbnailUrl = value;
            if (name.Equals("id"))
                person.Id = value;
            if (name.Equals("name"))
            {
                string nameData = getData(false, jsonText, "name");
                person.Name = getName(nameData);
            }
        }
        return person;
    }

    /// <summary>
    /// Return Total Results
    /// </summary>
    /// <param name="jsonText">Json Text</param>
    /// <returns></returns>
    public int getCount(string jsonText)
    {
        jObject = parser.Parse(jsonText);

        int messageCount = 0;
        foreach (JsonObject json in jObject as JsonObjectCollection)
        {
            string name = json.Name;
            if (name.Equals("totalResults"))
            {
                messageCount = int.Parse(json.GetValue().ToString());
                break;
            }
        }
        return messageCount;
    }

    /// <summary>
    /// Return All Friends
    /// </summary>
    /// <param name="jsonText">Json Text</param>
    /// <param name="no_of_friends">No. Of Friends</param>
    /// <returns></returns>
    public Person[] getFriends(string jsonText, int no_of_friends)
    {
        Person[] person = new Person[no_of_friends];
        int count = 0;

        jObject = parser.Parse(jsonText);
        foreach (JsonObjectCollection json in jObject as JsonArrayCollection)
        {
            person[count++] = getProfile(json.ToString());
        }

        return person;
    }

    /// <summary>
    /// Returns Message
    /// </summary>
    /// <param name="jsonText">Json Text</param>
    /// <returns></returns>
    public SocialMessage getMessage(string jsonText)
    {
        SocialMessage socialMessage = new SocialMessage();

        jObject = parser.Parse(jsonText);

        foreach (JsonObject json in jObject as JsonObjectCollection)
        {
            string name = json.Name;
            string value = json.GetValue().ToString();
            if (name.Equals("body"))
                socialMessage.Message = value;
            if (name.Equals("id"))
                socialMessage.MessageId = value;
            if (name.Equals("fromUserProfile"))
            {
                string personData = getData(false, jsonText, "fromUserProfile");
                socialMessage.MPerson = getProfile(personData);
            }
        }

        return socialMessage;
    }

    /// <summary>
    /// Returns All Messages
    /// </summary>
    /// <param name="jsonText">Json Text</param>
    /// <param name="no_of_messages">No. Of Messages</param>
    /// <returns></returns>
    public SocialMessage[] getMessages(string jsonText, int no_of_messages)
    {
        jObject = parser.Parse(jsonText);

        SocialMessage[] socialMessage = new SocialMessage[no_of_messages];
        int count = 0;

        foreach (JsonObjectCollection json in jObject as JsonArrayCollection)
        {
            socialMessage[count++] = getMessage(json.ToString());
        }

        return socialMessage;
    }
}
